package com.cloudkinto.cron;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.assist.ISqlRunner;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cloudkinto.common.DicBusinessItemConstant;
import com.cloudkinto.common.common.DateUtils;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.utils.ExcelUtils;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.dao.*;
import com.cloudkinto.entity.*;
import com.cloudkinto.service.ecang.ECangService;
import com.cloudkinto.service.email.MailSenderService;
import com.cloudkinto.service.finance.CompanyAccountService;
import com.cloudkinto.service.finance.CostRecordService;
import com.cloudkinto.service.finance.RechargeRecordService;
import com.cloudkinto.service.invoice.InvoiceRecordService;
import com.cloudkinto.service.mail.MailService;
import com.cloudkinto.service.order.OrderAmazonService;
import com.cloudkinto.service.order.OrderRakutenService;
import com.cloudkinto.service.order.OrderShopifyService;
import com.cloudkinto.service.order.impl.OrderAmazonServiceImpl;
import com.cloudkinto.service.order.impl.OrderRakutenServiceImpl;
import com.cloudkinto.service.orderback.OrderBackService;
import com.cloudkinto.service.orderoutput.OrderOutputService;
import com.cloudkinto.service.orderqoo.OrderQoo10Service;
import com.cloudkinto.service.orderqoo.impl.OrderQoo10ServiceImpl;
import com.cloudkinto.service.product.ProductStockService;
import com.cloudkinto.service.stock.StockLogService;
import com.cloudkinto.service.stock.StockPickUpService;
import com.cloudkinto.service.stock.vo.StockHistoryExport;
import com.cloudkinto.service.storage.StorageShelfService;
import com.cloudkinto.service.user.UserService;
import com.mysql.cj.log.Log;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.mail.Session;
import javax.mail.Transport;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangyy
 * @date 2020/6/18 16:03
 * @description: 开发环境的定时任务
 */
@ConditionalOnProperty(prefix = "gbc", name = "scheduling", havingValue = "dev")
@Component
@Logger
public class CronDevService {
    @Autowired
    private CompanyAccountService mCostService;
    @Autowired
    private StockRecordDao stockRecordDao;
    @Autowired
    private ProductInfoDao productInfoDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private StockLogService stockLogService;
    @Value("${gbc.uploadPath.filePathLocal}")
    private String filePath;
    @Autowired
    private MailSenderService mailSenderService;
    @Autowired
    private OrderShopifyService mShopifyOrderService;
    @Autowired
    private OrderOutputService orderOutput;
    @Autowired
    private OrderRakutenService mRakutenOrderService;
    @Autowired
    private OrderAmazonService mOrderAmzonService;
    @Autowired
    private InvoiceRecordService invoiceRecordService;
    @Autowired
    private MailService mailService;
    @Autowired
    private ProductStockService productStockService;
    @Autowired
    private OrderBackService orderBackService;
    @Autowired
    private ECangService eCangService;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private CompanyStoragePriceDao storagePriceDao;
    @Autowired
    private StockHistoryDetailDao stockHistoryDetailDao;
    @Autowired
    private StockHistoryDao stockHistoryDao;
    @Autowired
    private ExchangeRateDao mExchangeRateDao;
    @Autowired
    private CostRecordService costRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private StorageShelfService storageShelfService;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    private final org.slf4j.Logger logger = LoggerFactory.getLogger(CronDevService.class);
    @Autowired
    private OrderQoo10Service mOrderQoo10Service;
    @Autowired
    private RechargeRecordService mRechargeRecordSerivce;


    @Autowired
    private StockPickUpService stockPickUpService;
    /**
     * 定时生成波次
     */
    /**
     * 10分钟
     */
//    @Scheduled(initialDelay = 1000 * 20, fixedRate = 600000)
    @Scheduled(fixedRate = 600000)
    public void timingGeneratePickup() {
        stockPickUpService.generatePickup();
    }


    /**
     * 校验异常库位
     */
    @Scheduled(fixedRate = Integer.MAX_VALUE)
    public void rackMessageNotice() {
        storageShelfService.rackMessageNotice();
    }


    /**
     * 给所有公司 生成默认角色
     */
    @Scheduled(fixedRate = Integer.MAX_VALUE)
    public void createRoleForCompany() {
//        System.out.println("开始生成新角色");
//       orderOutput.autoUploadLasHourExpressNumber1();
//        System.out.println("结束生成新角色");
    }
//    @Scheduled(initialDelay = 1000 * 6, fixedRate = 1000 * 60 * 20)
//    public void autoSyncShippingOrder() {
//        eCangService.autoSyncShippingOrder();
//
//    }


    @Scheduled(initialDelay = 1000 * 6, fixedRate = 1000 * 60 * 5)
    @Async
    public void refreshShopifyOrder() {
//        mRechargeRecordSerivce.matchSmsList(
//        orderOutput.getExpressStatus("delivered");
        mOrderQoo10Service.autoRefreshOrder();
//        orderOutput.getExpressStatus("pickup");

//        orderOutput.sendYamatoSizeMail();
//        mShopifyOrderService.autoRefreshOrder();
//        orderOutput.autoUploadLasHourExpressNumber();
    }

    @Scheduled(fixedDelay = Long.MAX_VALUE)
    @Async
    public void sendRakutenMail() {
        try {
//            mRakutenOrderService.sendRakutenMail11Test(515L,202163);
        } catch (Exception e) {
            e.printStackTrace();
            //利用Java发送邮件
            Session session = mailSenderService.initMailSession();
            Transport transport = mailSenderService.initMailTransport(session);
            mailSenderService.sendEmail(session, transport, "大促邮件出错", "请联系管理员",
                    "rakuten_service@sanchoum.com");
            mailSenderService.transportClose(transport);
        }

    }

}